package com.ruoyi.net.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.BusiMtnGroupMapper;
import com.ruoyi.net.mapper.BusiMtnGroupPortMapper;
import com.ruoyi.net.mapper.ElementPortModeMapper;
import com.ruoyi.net.mapper.NetElementLinkMapper;
import com.ruoyi.net.service.IBusiMtnGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务-mtnGroup配置Service业务层处理
 * 
 * @author net
 * @date 2024-12-07
 */
@Service
public class BusiMtnGroupServiceImpl implements IBusiMtnGroupService 
{
    @Autowired
    private BusiMtnGroupMapper busiMtnGroupMapper;

    @Autowired
    private BusiMtnGroupPortMapper busiMtnGroupPortMapper;

    @Autowired
    private NetElementLinkMapper netElementLinkMapper;

    @Autowired
    private ElementPortModeMapper elementPortModeMapper;
    /**
     * 查询业务-mtnGroup配置
     * 
     * @param id 业务-mtnGroup配置主键
     * @return 业务-mtnGroup配置
     */
    @Override
    public BusiMtnGroup selectBusiMtnGroupById(String id)
    {
        return busiMtnGroupMapper.selectBusiMtnGroupById(id);
    }

    /**
     * 查询业务-mtnGroup配置列表
     * 
     * @param busiMtnGroup 业务-mtnGroup配置
     * @return 业务-mtnGroup配置
     */
    @Override
    public List<BusiMtnGroupListVo> selectBusiMtnGroupList(BusiMtnGroup busiMtnGroup)
    {
        return busiMtnGroupMapper.selectBusiMtnGroupList(busiMtnGroup);
    }

    /**
     * 新增业务-mtnGroup配置
     * 
     * @param busiMtnGroupParamVo 业务-mtnGroup配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBusiMtnGroup(BusiMtnGroupParamVo busiMtnGroupParamVo)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(busiMtnGroupParamVo.getId())) {
            busiMtnGroupParamVo.setId(uuid);
        }
        busiMtnGroupParamVo.setCreateBy(SecurityUtils.getUsername());
        busiMtnGroupParamVo.setCreateTime(DateUtils.getNowDate());
        busiMtnGroupParamVo.setUpdateBy(SecurityUtils.getUsername());
        busiMtnGroupParamVo.setUpdateTime(DateUtils.getNowDate());
        int i = busiMtnGroupMapper.insertBusiMtnGroup(busiMtnGroupParamVo);
        /**
         *新增MTN Group业务表端口信息
         * 1.获取网元间所有连线
         * 2.循环遍历连线两端端口模式，比较是否为MNT，如果是则新增业务表端口信息
         *
         */
        for (BusiMtnGroupPort busiMtnGroupPort : busiMtnGroupParamVo.getBusiMtnGroupPortList()) {
                busiMtnGroupPort.setId(IdUtils.simpleUUID());
                busiMtnGroupPort.setBusiMtnGroupId(busiMtnGroupParamVo.getId());
                busiMtnGroupPort.setCreateBy(SecurityUtils.getUsername());
                busiMtnGroupPort.setCreateTime(DateUtils.getNowDate());
                busiMtnGroupPortMapper.insertBusiMtnGroupPort(busiMtnGroupPort);
        }
        return i;
    }

    /**
     * 修改业务-mtnGroup配置
     * 
     * @param busiMtnGroup 业务-mtnGroup配置
     * @return 结果
     */
    @Override
    public int updateBusiMtnGroup(BusiMtnGroup busiMtnGroup)
    {
        busiMtnGroup.setUpdateBy(SecurityUtils.getUsername());
        busiMtnGroup.setUpdateTime(DateUtils.getNowDate());
        return busiMtnGroupMapper.updateBusiMtnGroup(busiMtnGroup);
    }

    /**
     * 批量删除业务-mtnGroup配置
     * 
     * @param ids 需要删除的业务-mtnGroup配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiMtnGroupByIds(String[] ids)
    {
        return busiMtnGroupMapper.deleteBusiMtnGroupByIds(ids);
    }

    /**
     * 删除业务-mtnGroup配置信息
     * 
     * @param id 业务-mtnGroup配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiMtnGroupById(String id)
    {
        return busiMtnGroupMapper.deleteBusiMtnGroupById(id);
    }

    @Override
    public List<NetElementLinkVo> caculatePhyList(BusiMtnGroup busiMtnGroup) {
        List<NetElementLinkVo> finalNetElementLinkVos = new ArrayList<>();
        List<NetElementLinkVo> allNetElementLinkVos = new ArrayList<>();

        // 获取当前节点有关的网元连线
        NetElementLink netElementLink = new NetElementLink();
        netElementLink.setStartGroupId(busiMtnGroup.getStartElement());
        netElementLink.setEndGroupId(busiMtnGroup.getEndElement());
        List<NetElementLinkVo> relationLinks = netElementLinkMapper.selectNetElementLinkVoList(netElementLink);
        //构造反向线
        netElementLink.setStartGroupId(busiMtnGroup.getEndElement());
        netElementLink.setEndGroupId(busiMtnGroup.getStartElement());
        List<NetElementLinkVo> relationLinks2 = netElementLinkMapper.selectNetElementLinkVoList(netElementLink);
        allNetElementLinkVos.addAll(relationLinks);
        allNetElementLinkVos.addAll(relationLinks2);
        for (NetElementLinkVo netElementLink1 : allNetElementLinkVos) {
            ElementPortMode startPortMode = elementPortModeMapper.selectElementPortModeById(netElementLink1.getStartPortId());
            ElementPortMode endPortMode = elementPortModeMapper.selectElementPortModeById(netElementLink1.getEndPortId());
            if ("disable".equals(startPortMode.getPortMode()) && "disable".equals(endPortMode.getPortMode())) {
                finalNetElementLinkVos.add(netElementLink1);
            }
        }
        return finalNetElementLinkVos;
    }

    @Override
    public List<BusiMtnGroupListVo> selectBusiMtnGroupEleList(BusiMtnGroupListVo busiMtnGroup) {
        List<BusiMtnGroupListVo> busiMtnGroupListVos = busiMtnGroupMapper.selectBusiMtnGroupEleList(busiMtnGroup);
        if (busiMtnGroup.getElementId()!=null){
            for (BusiMtnGroupListVo listVo : busiMtnGroupListVos){
                if (busiMtnGroup.getElementId().equals(listVo.getStartElement())){
                    listVo.setGroupIndex(listVo.getStartElementGroupIndex());
                } else if(busiMtnGroup.getElementId().equals(listVo.getEndElement())){
                    listVo.setGroupIndex(listVo.getEndElementGroupIndex());
                }
            }
        }
        return busiMtnGroupListVos;
    }
}
